#include <iostream>
#include <GL/glut.h>

#include "View.h"
#include "Configuration.h"
#include "Collection.h"

using namespace std;

//Collection<SpaceShip> View::friendlyObjects;
//Collection<InteractiveObject> View::enemyObjects;
//Collection<BaseObject> View::staticObjects;

View::View()
	{

	};

void View::DisplayFrame(void)
	{ 		
		glClearColor(0.0f,0.0f,0.0f,0.0f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glLoadIdentity();
		SetupOpenGL();						

		for (int i = 0; i < friendlyObjects->Count(); i++)
		{
			friendlyObjects->Get(i)->Display();
		}	
		for (int i = 0; i < enemyObjects->Count(); i++)
		{
			enemyObjects->Get(i)->Display();
		}
		for (int i = 0; i < staticObjects->Count(); i++)
		{
			staticObjects->Get(i)->Display();
		}
		
		glutSwapBuffers();
	};

void View::SetupOpenGL()
{	
	glEnable (GL_DEPTH_TEST);
	glEnable (GL_NORMALIZE);
	glEnable ( GL_TEXTURE_2D );
	glShadeModel (GL_SMOOTH);
	glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
	glColorMaterial (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
	glAlphaFunc ( GL_GREATER, 0.01f ) ;
    glEnable ( GL_ALPHA_TEST ) ;
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
}

void View::Reshape(int width, int height)
	{
		Config::ScreenWidth = width;
		Config::ScreenHeight = height;
		glViewport(0,0,(GLsizei)width,(GLsizei)height);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(60,(GLfloat)width/(GLfloat)height,1.0,1000.0);
		glMatrixMode(GL_MODELVIEW);
	}

/// Initialising the diplay.
/// Configuring display options according to selected settings.
void View::Initialise(Collection<SpaceShip>* friendlyObjects,Collection<InteractiveObject>* enemyObjects,Collection<BaseObject>* staticObjects)
{	
	this->friendlyObjects = friendlyObjects;	
	this->enemyObjects = enemyObjects;	
	this->staticObjects = staticObjects;
	glutInitDisplayMode(GLUT_DOUBLE| GLUT_RGBA | GLUT_ALPHA | GLUT_DEPTH);
	if (Config::Fullscreen)
	{
		glutGameModeString("1680x1050:32@60");
		glutEnterGameMode();
	}
	else
	{
		glutInitWindowSize(Config::ScreenWidth,Config::ScreenHeight);	
		glutInitWindowPosition(100,100);
		glutCreateWindow("The Game");
	}
	glutReshapeFunc(&View::Reshape);		
}


